package com.cs4261.smartalarm.frontend;

import com.cs4261.smartalarm.R;
import com.cs4261.smartalarm.backend.Alarm;
import com.cs4261.smartalarm.backend.AlarmsDatabaseHelper;

import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewParent;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.support.v4.app.NavUtils;

public class CreateAlarmActivity extends Activity {
	
	public AlarmsDatabaseHelper alDBHelper;
	public Alarm newAlarm;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_create_alarm);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Show the Up button in the action bar.
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
		
		alDBHelper = new AlarmsDatabaseHelper(getApplicationContext());
		newAlarm = new Alarm();
		
		Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
		if (RingtoneManager.getRingtone(getApplicationContext(), uri) == null) {
			// alert is null, using backup
			uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
			if (RingtoneManager.getRingtone(getApplicationContext(), uri) == null) {
				// alert backup is null, using 2nd backup
				uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
			}
		}
		
		newAlarm.setTone(uri.toString());
		
		((TextView)findViewById(R.id.view_toneName)).setText(RingtoneManager.getRingtone(this, uri).getTitle(this));
	}
	
	public void cancelCreate(View view) {
		NavUtils.navigateUpFromSameTask(this);
	}
	
	public void createAlarm(View view){
		newAlarm.setName(((EditText) findViewById(R.id.field_name)).getText().toString());
		//Time
		newAlarm.setHour(((TimePicker) findViewById(R.id.field_time)).getCurrentHour());
		newAlarm.setMinute(((TimePicker) findViewById(R.id.field_time)).getCurrentMinute());
		//Days
		newAlarm.setRepeat(((ToggleButton) findViewById(R.id.toggle_repeat)).isChecked() == true ? 1 : 0);
		newAlarm.setSunday(((ToggleButton) findViewById(R.id.toggle_sunday)).isChecked() == true ? 1 : 0);
		newAlarm.setMonday(((ToggleButton) findViewById(R.id.toggle_monday)).isChecked() == true ? 1 : 0);
		newAlarm.setTuesday(((ToggleButton) findViewById(R.id.toggle_tuesday)).isChecked() == true ? 1 : 0);
		newAlarm.setWednesday(((ToggleButton) findViewById(R.id.toggle_wednesday)).isChecked() == true ? 1 : 0);
		newAlarm.setThursday(((ToggleButton) findViewById(R.id.toggle_thursday)).isChecked() == true ? 1 : 0);
		newAlarm.setFriday(((ToggleButton) findViewById(R.id.toggle_friday)).isChecked() == true ? 1 : 0);
		newAlarm.setSaturday(((ToggleButton) findViewById(R.id.toggle_saturday)).isChecked() == true ? 1 : 0);
		//Weather
		newAlarm.setWeatherActive(((ToggleButton) findViewById(R.id.toggle_weather)).isChecked() == true ? 1 : 0);
		newAlarm.setW1(Integer.parseInt(((EditText)findViewById(R.id.delay_clear)).getText().toString()));
		newAlarm.setW2(Integer.parseInt(((EditText)findViewById(R.id.delay_fog)).getText().toString()));
		newAlarm.setW3(Integer.parseInt(((EditText)findViewById(R.id.delay_rain)).getText().toString()));
		newAlarm.setW4(Integer.parseInt(((EditText)findViewById(R.id.delay_snow)).getText().toString()));
		//Traffic
		newAlarm.setTrafficActive(((ToggleButton) findViewById(R.id.toggle_traffic)).isChecked() == true ? 1 : 0);
		newAlarm.setLocation1(Long.parseLong(((EditText)findViewById(R.id.field_startLocation)).getText().toString()));
		newAlarm.setLocation2(Long.parseLong(((EditText)findViewById(R.id.field_endLocation)).getText().toString()));
		//Snooze duration
		newAlarm.setSnooze(Integer.parseInt(((EditText)findViewById(R.id.field_snooze)).getText().toString()));
		//Tone handled in onCreate/chooseTone
		
		alDBHelper.open();
		long id = alDBHelper.createAlarm(newAlarm);
		newAlarm.setId(id);
		alDBHelper.close();
		
		Toast.makeText(getApplicationContext(), newAlarm.getId() + "   " + newAlarm.getName(), Toast.LENGTH_LONG).show();
		
		NavUtils.navigateUpFromSameTask(this);
	}
	
	public void repeatToggle(View view) {
		 boolean on = ((ToggleButton)view).isChecked();
		 View layout = ((View) view.getParent().getParent()).findViewById(R.id.layout_repeat);
		 
		 if(on) {
			 layout.setVisibility(View.VISIBLE);
		 }
		 else {
			 layout.setVisibility(View.GONE);
		 }
	}
	
	public void weatherToggle(View view) {
		 boolean on = ((ToggleButton)view).isChecked();
View layout = ((View) view.getParent().getParent()).findViewById(R.id.layout_weather);
		 
		 if(on) {
			 layout.setVisibility(View.VISIBLE);
		 }
		 else {
			 layout.setVisibility(View.GONE);
		 }
	}
	
	public void trafficToggle(View view) {
		 boolean on = ((ToggleButton)view).isChecked();
View layout = ((View) view.getParent().getParent()).findViewById(R.id.layout_traffic);
		 
		 if(on) {
			 layout.setVisibility(View.VISIBLE);
		 }
		 else {
			 layout.setVisibility(View.GONE);
		 }
	}
	
	public void chooseTone(View view) {
		Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
		intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_ALARM);
		intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
		intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
		this.startActivityForResult(intent, 5);
	}
	
	@Override
	protected void onActivityResult(final int requestCode,
			final int resultCode, final Intent intent) {
		String chosenTone;
		if (resultCode == Activity.RESULT_OK && requestCode == 5) {
			Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
			if (uri != null) {
				chosenTone = uri.toString();
				newAlarm.setTone(chosenTone);
				((TextView)findViewById(R.id.view_toneName)).setText(RingtoneManager.getRingtone(this, uri).getTitle(this));
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_create_alarm, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
